package top.crossrun.webview.bridge.webview.SysWebView;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import android.widget.ProgressBar;

import top.crossrun.webview.bridge.core.R;
import top.crossrun.webview.bridge.core.utils.BridgeHelper;
import top.crossrun.webview.bridge.core.utils.BridgeUtil;
import top.crossrun.webview.bridge.core.utils.RawJSEval;

/**
 * create by Run
 * on 2020/10/13 09:07
 * do
 */
public class SysWebViewActivity extends Activity {
    protected FrameLayout fullScreenLayout;
    protected WebView webView;
    protected ProgressBar progressBar;
    String url;

    protected BridgeHelper bridgeHelper;

    protected SysWebViewChromeClient sysWebViewChromeClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_sys);
        initIntentData();
        initView();
        initWebView();
        initPlugin();
    }

    /**
     * findview
     */
    public void initView() {
        fullScreenLayout = findViewById(R.id.fullscreen);
        webView = findViewById(R.id.webview);
        progressBar = findViewById(R.id.progress);
    }

    /**
     * get data from intent
     */
    public void initIntentData() {
        Intent data = getIntent();
        if (data == null) {
            return;
        }
        url = getIntent().getStringExtra("url");
    }

    /**
     * init webview
     */
    public void initWebView() {
        sysWebViewChromeClient = new SysWebViewChromeClient(this, 1, fullScreenLayout, progressBar);

        bridgeHelper = new BridgeHelper(this, new RawJSEval(webView));

        SysWebViewHelper.initWebView(webView);

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
//                Log.e("console","over url = "+url);
                if (bridgeHelper.shouldOverrideUrlLoading(BridgeUtil.decode(url), true)) {
                    return true;
                }
                if (url.startsWith("http") || url.startsWith("file:///")) {
                    view.loadUrl(url);
                }
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                bridgeHelper.onPageFinished(url);
            }
        });
    }

    /**
     * init plugin
     */
    public void initPlugin() {
    }

    public void loadUrl(){
        if (!TextUtils.isEmpty(url)) {
            webView.loadUrl(url);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        bridgeHelper.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        bridgeHelper.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    protected void onResume() {
        super.onResume();
        bridgeHelper.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        bridgeHelper.onPause();
    }

    @Override
    protected void onStart() {
        super.onStart();
        bridgeHelper.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        bridgeHelper.onStop();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        bridgeHelper.onDestroy();
        destoryWebView();
    }

    /**
     * destory webview
     */
    public void destoryWebView() {
        try {
            ViewParent parent = webView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(webView);
            }
            webView.stopLoading();
            // 退出时调用此方法，移除绑定的服务，否则某些特定系统会报错
            webView.getSettings().setJavaScriptEnabled(false);
            webView.clearHistory();
            webView.removeAllViews();

            webView.destroy();
        } catch (Throwable ex) {

        }
        webView = null;
    }
}
